package com.abel.web.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.abel.domain.Person;
import com.abel.domain.SearchCriteria;
import com.abel.manager.SearchManager;
import com.abel.web.view.SearchViewBean;

public class SearchController extends SimpleFormController {

	protected final Log logger = LogFactory.getLog(getClass());
	
	private SearchManager searchManager;
	
	
	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		
		logger.debug("@SearchController : onSubmit : entered");
		
		//get the search criteria
		SearchViewBean viewBean = (SearchViewBean)command;
		ModelAndView mav = null;
		//setting attributes for manager
		SearchCriteria criteria = new SearchCriteria();
		
		if (viewBean.getDateLastSevenDays() != null) {
			
			Calendar priorSevenDay = Calendar.getInstance();
			priorSevenDay.add(Calendar.DATE, -7);
			criteria.setDateSubmittedBegin(priorSevenDay.getTime());
			criteria.setDateSubmittedEnd(new Date());
		}
		else {
			DateFormat format = new SimpleDateFormat("MM/dd/yyyy");
			criteria.setLastName(viewBean.getLastName());
			criteria.setSsn(viewBean.getSsn());
			if (viewBean.getDateSubmittedBegin() != null 
					&& !viewBean.getDateSubmittedBegin().equals("")) {
				
				criteria.setDateSubmittedBegin(format.parse(viewBean.getDateSubmittedBegin()));
			}
			
			if (viewBean.getDateSubmittedEnd() != null 
					&& !viewBean.getDateSubmittedEnd().equals("")) {
				
				criteria.setDateSubmittedEnd(format.parse(viewBean.getDateSubmittedEnd()));
			}
		}
		
		//fetching search results
		try {

			List<Person> applications = this.searchManager.getApplications(criteria);
			
			//setting data in the modeland view to be retreived in the view page
			viewBean.setApplications(applications);
			mav = new ModelAndView(getSuccessView());
			Map<String, Object> modelMap = new HashMap<String, Object>();
			modelMap.put("searchView", viewBean);
			mav.addAllObjects(modelMap);
		} 
		catch (Exception e) {
			
			logger.error("@SearchController : onSubmit : Exception while registering" + e);
			e.printStackTrace();
			request.setAttribute("registrationView", viewBean);
			errors.rejectValue("", "error.null",null,"A system error occurred while searching for apps.  Please logout and log back in");
			return showForm(request,response,errors);
		}
		
		
		return mav;
	}

	
	private ModelAndView processSubmitToResumate (HttpServletRequest request)
	throws Exception {
		return null;
	}
	
	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		

		return new SearchViewBean();
	}


	/**
	 * @param searchManager the searchManager to set
	 */
	public void setSearchManager(SearchManager searchManager) {
		this.searchManager = searchManager;
	}

	
	
}
